import AdminConfig from "@/admin.config.js";

import ToolVuex from "@/tool/vuex/ToolVuex.js";
import ToolUni from "../ToolUni";

export default {
	async request(url, data = {}, method, headers) {
		if (url.indexOf("/") !== 0 && url.indexOf("http") !== 0) {
			url = "/" + url;
		}
		if (url.indexOf("http") !== 0) {
			url = `${AdminConfig.baseApi}${url}`;
		}
		if (url.indexOf("?") < 0) {
			url += `?_t=${new Date().getMinutes()}`
		}
		let _dsId = data._dsId || ToolVuex.state._dsId;
		url += _dsId ? `&_dsId=${_dsId}` : "";
		delete data._dsId;
		let res = await uni.request({
			url,
			data,
			method,
			header: {
				ntd_token: "ntd_fmhb",
				...headers
			}
		});
		if (res.data.code !== 500) {
			return res.data.data || res.data;
		}
		ToolUni.toast(res.data.msg || "服务器繁忙，请您稍后重试...");
	},
	get(url, data, headers) {
		return this.request(url, data, headers);
	},
	post(url, data, headers) {
		return this.request(url, data, "POST", headers);
	},
	put(url, data, headers) {
		return this.request(url, data, "PUT", headers);
	},
	delete(url, data, headers) {
		return this.request(url, data, "DELETE", headers);
	}
}
